#include <cstdlib>
#include <cstring>
#include <algorithm>

#include "array.h"

//const size_t DEFAULT_CAPACITY = 2;

Array::Array(size_t size) : _size(size), _capacity(size) { 
	_ar = new int [_capacity]; 
}

Array::Array() : _size(0), _capacity(DEFAULT_CAPACITY) { 
	_ar = new int [_capacity]; 
}

Array::Array(const Array &rhs) : _size(rhs._size), _capacity(rhs._capacity) { 
	_ar = new int[_size]; 
	memcpy(_ar, rhs._ar, _size * sizeof(int)); 
}

Array::~Array() { 
	delete [] _ar; 
	_ar = NULL; 
}

int Array::get(size_t index) const { 
	return _ar[index]; 
}

void Array::set(size_t index, int value) { 
	_ar[index]=value; 
}

void Array::swap(Array &obj) {
	std::swap(_capacity, obj._capacity);
	std::swap(_size, obj._size);
	std::swap(_ar, obj._ar);
}

void Array::push_back(int v) {
	if (_size >= _capacity) { 
		_capacity *= 2;
		int *temp_ar = new int [_capacity];
		memcpy(temp_ar, _ar, _size * sizeof(int)); 
		std::swap(temp_ar, _ar);
		delete [] temp_ar;
	}

	_ar[_size] = v;
	++_size;
}

Array& Array::operator= (const Array &rhs) {
	Array temp(rhs);
	swap(temp);
	return *this;
}

size_t Array::size() const {
	return _size;
}
